//
//  ViewController.m
//  UI13-段子
//
//  Created by dllo on 16/6/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Model.h"
#import "MyCell.h"
#import "LableCell.h"
#import "GetHightTools.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *array;
@end

@implementation ViewController
static NSString *const MyCellID = @"mycell";
static NSString *const LableCellID = @"LableCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Mine1.png"] style:UIBarButtonItemStyleDone target:self action:nil];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"段子";
    [self createData];
    [self createView];
}
- (void)createData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"duanzi" ofType:@"txt"];
    NSData *data =[NSData dataWithContentsOfFile:path];
    NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSArray *array = [dic valueForKey:@"段子"];
    self.array = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        Model *model = [[Model alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.array addObject:model];
    }
}

- (void)createView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor yellowColor];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 210;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    [_tableView release];
    [self.tableView registerClass:[MyCell class] forCellReuseIdentifier:MyCellID];
    [self.tableView registerClass:[LableCell class] forCellReuseIdentifier:LableCellID];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Model *model = self.array[indexPath.row];
    if (model.img.length == 0) {
        LableCell *cell = [tableView dequeueReusableCellWithIdentifier:LableCellID];
        cell.model = model;
        return cell;
    }else{
        MyCell *cell =[tableView dequeueReusableCellWithIdentifier:MyCellID];
        cell.model = model;
        return cell;
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Model *model = self.array[indexPath.row];
    CGFloat height = [GetHightTools getHeight:model.digest width:414 font:[UIFont systemFontOfSize:17]];
    if (model.img.length == 0) {
        return height + 10;
    }else{
        CGFloat imageWidth = [model.pixel substringToIndex:3].floatValue;
        CGFloat imageHeight =  [model.pixel substringFromIndex:4].floatValue;
        CGFloat screenHeight = 414 * imageHeight / imageWidth;
        return screenHeight + height + 10;
    }
}





























































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
